package pl.edu.pw.ee.buildcity.services;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pl.edu.pw.ee.buildcity.commons.Const;
import pl.edu.pw.ee.buildcity.domain.BuildStatus;
import pl.edu.pw.ee.buildcity.scm.subversion.SVNCheckoutParameters;
import pl.edu.pw.ee.buildcity.scm.subversion.SubversionService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;

import static org.fest.assertions.Assertions.assertThat;
import static pl.edu.pw.ee.buildcity.domain.BuildStatus.FAILED;
import static pl.edu.pw.ee.buildcity.domain.BuildStatus.SUCCEEDED;

/**
 * @author mgrzechocinski
 * @since 2009-09-20
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:maven-builder-context.xml", "classpath:scm-context.xml", "classpath:core-context.xml", "classpath:test-context.xml"})
public class MavenBuilderIntegrationTestCase {

	@Resource(name = "mavenBuilder")
	private MavenBuilderService mavenBuilderService;

	@Resource(name = "subversionService")
	private SubversionService svnService;

	private static final String TEST_PROJECT = "http://buildcity.googlecode.com/svn/trunk/wordcounter";

	private File workingCopy;

	@PostConstruct
	public void checkoutWorkingCopy() {
		SVNCheckoutParameters checkoutParameters = SVNCheckoutParameters.checkoutHEAD(TEST_PROJECT);
		workingCopy = svnService.checkout(checkoutParameters, Const.WORKING_COPIES + checkoutParameters.pathToCheckout);
	}

	@Test
	public void shouldSucceedMavenBuild() {
		//Given
		String goodBuildCommand = "clean install";

		//When
		BuildStatus buildStatus = mavenBuilderService.build(workingCopy, goodBuildCommand);

		//Then
		assertThat(buildStatus).isEqualTo(SUCCEEDED);
	}

	@Test
	public void shouldFailMavenBuildAsCommandIsWrong() {
		//Given
		String wrongBuildCommand = "clean instaL";

		//When
		BuildStatus buildStatus = mavenBuilderService.build(workingCopy, wrongBuildCommand);

		//Then
		assertThat(buildStatus).isEqualTo(FAILED);
	}

	@Test
	public void shouldFailMavenBuildAsCommandIsEmpty() {
		//Given
		String emptyBuildCommand = "   ";

		//When
		BuildStatus buildStatus = mavenBuilderService.build(workingCopy, emptyBuildCommand);

		//Then
		assertThat(buildStatus).isEqualTo(FAILED);
	}
}
